package com.deayalar.rodandov2.view.fragments;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.deayalar.rodandov2.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.turbomanage.httpclient.AsyncCallback;
import com.turbomanage.httpclient.HttpResponse;
import com.turbomanage.httpclient.android.AndroidHttpClient;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;




public class WorkShopFragment extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks,
														  GooglePlayServicesClient.OnConnectionFailedListener {
	
	
	GoogleMap mMap;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.work_shop_fragment, container, false);
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        AndroidHttpClient httpClient = new AndroidHttpClient("http://rodandoapi.appspot.com");
		httpClient.setMaxRetries(2);
		httpClient.get("/workshop/list", null, new AsyncCallback() {
		    @Override
		    public void onComplete(HttpResponse httpResponse) {
		    	String response = httpResponse.getBodyAsString();
		        try {
					JSONObject responseJson = new JSONObject(response);
					String workshopsArrayString = responseJson.getString("response");
					JSONArray array = new JSONArray(workshopsArrayString);
					
					for (int i = 0; i < array.length(); i++) {
						
					    JSONObject row = array.getJSONObject(i);
					    double lat = Double.valueOf(row.getString("lat"));
					    double lng = Double.valueOf(row.getString("lng"));	
					    
					    int resourceId = R.drawable.wheel;
					    
					    MarkerOptions marker = new MarkerOptions()
										        .position(new LatLng(lat, lng))
										        .title(row.getString("name"))
										        .snippet(row.getString("address") + " \n " + row.getString("score"));
					    
					    mMap.addMarker(marker);
					    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(4.6832081, -73.9988203), 10);
					    mMap.animateCamera(cameraUpdate);
					}
					
					
				} catch (JSONException e) {
					Log.e("deayalar", e.getMessage(), e);
				}	         
		    }
		    
			@Override
		    public void onError(Exception e) {
		        e.printStackTrace();
		    }
		});		
        return rootView;
}
	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onConnected(Bundle arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		
	}

	
	public void onDestroyView() {
	    super.onDestroyView();
	    if (mMap != null) {
	        getFragmentManager()
	                .beginTransaction()
	                .remove(getFragmentManager().findFragmentById(R.id.map))
	                .commit();
	    }
	}
}
